<?php
namespace app\common\service;

use think\Model;

class CourseClassService extends BaseService
{

    /**
     * 增加课程班级关联关系
     * @param int $course_id
     * @param array $class_ids
     * @return boolean
     */
    public function addCourseClass($course_id,$class_ids){
        $course_class = db('course_class')->where(['courseid' => $course_id])->select();

        if(!empty($class_ids) && is_array($class_ids)){
            $course_class_data = [];

            //如果查询到之前的数据，则先删除后插入  简单粗暴   并非最佳方案
            if($course_class){
                db('course_class')->where(['courseid' => $course_id])->delete();
            }

            foreach ($class_ids as $k => $v){
                $course_class_data[$k]['classid'] = $v;
                $course_class_data[$k]['courseid'] = $course_id;
                $course_class_data[$k]['addtime'] = time();
            }

            $result = db('course_class')->insertAll($course_class_data);
        } elseif (empty($class_ids) && !empty($course_class)){
            //全部删除
            $result = db('course_class')->where(['courseid' => $course_id])->delete();
        }

        return $result;
    }

}







